<!DOCTYPE html>
<html>
  <head>
    <title>RTCPeerConnection.createOffer</title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
  </head>
  <body>
    <script>
// These tests are testing the non-standard "sdpFormat" extension.
promise_test(async t => {
  const pc = new RTCPeerConnection({sdpSemantics: "plan-b"});
  t.add_cleanup(() => pc.close());
  return navigator.mediaDevices.getUserMedia({audio:true})
    .then(stream => {
      stream2 = stream.clone();
      pc.addTrack(stream.getAudioTracks()[0]);
      pc.addTrack(stream2.getAudioTracks()[0]);
      return pc.createOffer();
    })
    .then(offer => {
      let section_count = offer.sdp.match(/\nm=audio /g || []).length;
      assert_equals(section_count, 1);
    });
}, 'Plan B createOffer creates only one media section for two audio tracks');

promise_test(async t => {
  const pc = new RTCPeerConnection({sdpSemantics: "unified-plan"});
  t.add_cleanup(() => pc.close());
  return navigator.mediaDevices.getUserMedia({audio:true})
    .then(stream => {
      stream2 = stream.clone();
      pc.addTrack(stream.getAudioTracks()[0]);
      pc.addTrack(stream2.getAudioTracks()[0]);
      return pc.createOffer();
    })
    .then(offer => {
      let section_count = (offer.sdp.match(/\nm=audio /g) || []).length;
      assert_equals(section_count, 2);
    });
}, 'Unified plan createOffer for audio creates two media sections ' +
   'for two audio tracks');

promise_test(async t => {
  const pc = new RTCPeerConnection({sdpSemantics: "plan-b"});
  t.add_cleanup(() => pc.close());
  return navigator.mediaDevices.getUserMedia({video:true})
    .then(stream => {
      stream2 = stream.clone();
      pc.addTrack(stream.getVideoTracks()[0]);
      pc.addTrack(stream2.getVideoTracks()[0]);
      return pc.createOffer();
    })
    .then(offer => {
      let section_count = offer.sdp.match(/\nm=video /g || []).length;
      assert_equals(section_count, 1);
    });
}, 'Plan B createOffer for video creates one media section for two ' +
   'video tracks');

promise_test(async t => {
  const pc = new RTCPeerConnection({sdpSemantics: "unified-plan"});
  t.add_cleanup(() => pc.close());
  return navigator.mediaDevices.getUserMedia({video:true})
    .then(stream => {
      stream2 = stream.clone();
      pc.addTrack(stream.getVideoTracks()[0]);
      pc.addTrack(stream2.getVideoTracks()[0]);
      return pc.createOffer();
    })
    .then(offer => {
      let section_count = offer.sdp.match(/\nm=video /g || []).length;
      assert_equals(section_count, 2);
    });
}, 'Unified plan createOffer for video creates two media sections ' +
'for two video tracks');

// TODO(hta): Add a test case with both audio and video tracks.

promise_test(async t => {
  const pc1 = new RTCPeerConnection({sdpSemantics: "plan-b"});
  t.add_cleanup(() => pc1.close());
  const pc2 = new RTCPeerConnection({sdpSemantics: "plan-b"});
  t.add_cleanup(() => pc2.close());
  return navigator.mediaDevices.getUserMedia({video: true})
    .then(stream => {
      pc1.addTrack(stream.getVideoTracks()[0]);
      return pc1.createOffer();
    })
    .then(offer => {
      return Promise.all([pc2.setRemoteDescription(offer),
                          pc1.setLocalDescription(offer)]);
    })
    .then(() => {
      return pc2.createAnswer();
    })
    .then(answer => {
      return Promise.all([pc2.setLocalDescription(answer),
                          pc1.setRemoteDescription(answer)]);
    });
}, 'Plan B can connect to itself');

promise_test(async t => {
  const pc1 = new RTCPeerConnection({sdpSemantics: "unified-plan"});
  t.add_cleanup(() => pc1.close());
  const pc2 = new RTCPeerConnection({sdpSemantics: "unified-plan"});
  t.add_cleanup(() => pc2.close());
  return navigator.mediaDevices.getUserMedia({video: true})
    .then(stream => {
      pc1.addTrack(stream.getVideoTracks()[0]);
      return pc1.createOffer();
    })
    .then(offer => {
      return Promise.all([pc2.setRemoteDescription(offer),
                          pc1.setLocalDescription(offer)]);
    })
    .then(() => {
      return pc2.createAnswer();
    })
    .then(answer => {
      return Promise.all([pc2.setLocalDescription(answer),
                          pc1.setRemoteDescription(answer)]);
    });
}, 'Unified Plan can connect to itself');

promise_test(async t => {
  const pc1 = new RTCPeerConnection({sdpSemantics: "unified-plan"});
  t.add_cleanup(() => pc1.close());
  const pc2 = new RTCPeerConnection({sdpSemantics: "plan-b"});
  t.add_cleanup(() => pc2.close());
  return navigator.mediaDevices.getUserMedia({video: true})
    .then(stream => {
      pc1.addTrack(stream.getVideoTracks()[0]);
      return pc1.createOffer();
    })
    .then(offer => {
      return Promise.all([pc2.setRemoteDescription(offer),
                          pc1.setLocalDescription(offer)]);
    })
    .then(() => {
      return pc2.createAnswer();
    })
    .then(answer => {
      return Promise.all([pc2.setLocalDescription(answer),
                          pc1.setRemoteDescription(answer)]);
    });

}, 'Unified plan with one video track can connect to Plan B');

promise_test(async t => {
  pc1 = new RTCPeerConnection({sdpSemantics: "plan-b"});
  t.add_cleanup(() => pc1.close());
  pc2 = new RTCPeerConnection({sdpSemantics: "unified-plan"});
  t.add_cleanup(() => pc2.close());
  return navigator.mediaDevices.getUserMedia({video: true})
    .then(stream => {
      pc1.addTrack(stream.getVideoTracks()[0]);
      return pc1.createOffer();
    })
    .then(offer => {
      return Promise.all([pc2.setRemoteDescription(offer),
                          pc1.setLocalDescription(offer)]);
    })
    .then(() => {
      return pc2.createAnswer();
    })
    .then(answer => {

      return Promise.all([pc2.setLocalDescription(answer),
                          pc1.setRemoteDescription(answer)]);
    });

}, 'Plan B with one video track can connect to Unified plan');

promise_test(async t => {
  pc1 = new RTCPeerConnection({sdpSemantics: 'unified-plan'});
  t.add_cleanup(() => pc1.close());
  pc2 = new RTCPeerConnection({sdpSemantics: 'plan-b'});
  t.add_cleanup(() => pc2.close());
  pc1.addTransceiver('video');
  pc1.addTransceiver('video');
  await pc2.setRemoteDescription(await pc1.createOffer());
  assert_equals(pc1.getReceivers().length, 2);
  assert_equals(pc2.getReceivers().length, 1);
}, 'Plan B rejects additional m-lines without throwing an error');
    </script>
  </body>
</html>
